<template>
	<view style="padding:20rpx">
		<u--form
			labelPosition="left"
			:model="userInfo"
			:rules="rules"
			ref="form1"
			labelWidth="80"
		>
			<u-form-item
				label="原密码"
				prop="oldpwd"
				borderBottom
				ref="item1"
			>
				<u--input
					v-model="userInfo.oldpwd"
					border="none"
					placeholder="请输入密码"
				></u--input>
			</u-form-item>
			<u-form-item
				label="新密码"
				prop="newpwd"
				borderBottom
				ref="item1"
			>
				<u--input
					v-model="userInfo.newpwd"
					border="none"
					placeholder="请输入密码"
				></u--input>
			</u-form-item>
			<u-form-item
				label="确认密码"
				prop="snewpwd"
				borderBottom
				ref="item1"
			>
				<u--input
					v-model="userInfo.snewpwd"
					border="none"
					placeholder="请输入密码"
				></u--input>
			</u-form-item>
		</u--form>
		<view style="width:96%;position:fixed;bottom:20rpx;left:50%;right:50%; transform: translate(-50%, -50%);" @click="updatePwd">
		  <u-button  color="#1E8550" round size="large">确定</u-button>
		</view>
	</view>

</template>

<script>
	import Config from '@/util/index.js'
	import {
	  userEditPwd
	} from '@/api/user'
	export default {
		data() {
		    return {
				oldpwd:'',
				newpwd:'',
				snewpwd:'',
				user:{},
				userInfo:{
					oldpwd:'',
					newpwd:'',
					snewpwd:''
				},
				oldpwd:"",
				formPage:0,
				rules:{
					'oldpwd': {
						type: 'string',
						required: true,
						message: '请填写密码',
						trigger: ['blur', 'change']
					},
					'newpwd': {
						type: 'string',
						required: true,
						message: '请填写密码',
						trigger: ['blur', 'change']
					},
					'snewpwd': {
						type: 'string',
						required: true,
						message: '请填写密码',
						trigger: ['blur', 'change']
					}
				}
		    }
		},
		onLoad(options){
			let _this = this
			uni.getStorage({
				key: 'userifo',
				success: function (res) {
					let userIfo = JSON.parse(res.data)
					_this.oldpwd = userIfo.password
				}
			})
		},
		methods:{
			updatePwd(){
			    let params = {
			      loginid:uni.getStorageSync('userId'),
				  oldPassword:this.userInfo.oldpwd,
			      newPassword:this.userInfo.newpwd
			    }
				if(this.userInfo.oldpwd=="" || this.userInfo.newpwd=="" || this.userInfo.snewpwd==""){
				    wx.showToast({
				        title:"密码不能为空",
				        icon: 'none',
				        duration: 2000
				    });
				    return false
				}
			    if(this.userInfo.oldpwd != this.oldpwd){
					uni.showToast({
						title:"原密码输入错误！",
						icon: 'none',
						duration: 2000
					});
					return false
			    }
			    if(this.userInfo.snewpwd != this.userInfo.newpwd){
					uni.showToast({
						title:"两次密码不一样！",
						icon: 'none',
						duration: 2000
					});
			      return false
			    }
				
			    let _this = this
			    userEditPwd(params).catch(res=> {
			      if(res.retCode == 0){
			        uni.showToast({
			          title:res.info,
			          icon: 'none',
			          duration: 2000
			        });
			      }else{
			        uni.showToast({
			          title:res.info,
			          icon: 'none',
			          duration: 2000
			        });
			      }
			    })
			}
		}
	}
</script>

<style>
	.outlogin{width:96%;height:80rpx;position:fixed;bottom:60rpx;left:auto;right:auto;}
</style>
